import cn.hutool.core.io.file.FileAppender;
import com.alibaba.fastjson.JSONObject;
import com.github.javafaker.Faker;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TestCsvFileBuilder {
    public static void main(String[] args) throws IOException {
//        List<String> list = new ArrayList<>();
//        FileWriter writer = new FileWriter("C:\\Users\\liujing\\Desktop\\testWrite.csv");
        File file = new File("C:\\Users\\liujing\\Desktop\\testWrite.csv");
        FileAppender appender = new FileAppender(file, 16, true);
        appender.append("alarm");

//        CsvWriter writer = CsvUtil.getWriter("C:\\Users\\liujing\\Desktop\\testWrite.csv", CharsetUtil.CHARSET_UTF_8);

        List<String> list = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            Faker faker = new Faker();
            String alarmId = String.valueOf(faker.number().randomNumber());
            Alarms alarms = new Alarms();
            alarms.setAlarmId(alarmId);
            alarms.setAlarmType(String.valueOf(faker.number().numberBetween(1, 100)));
            alarms.setEventType(String.valueOf(faker.number().numberBetween(1, 100)));
            alarms.setSenderId(String.valueOf(faker.number().randomNumber()));
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(currentTime);
            alarms.setSentDateTime(dateString);
            alarms.setSeverity(faker.number().digits(1));
            alarms.setSource(faker.zelda().character());
            alarms.setAlarmState(faker.number().digits(1));
            alarms.setResourcePath(faker.address().streetName());
            alarms.setNote(faker.leagueOfLegends().champion());
            alarms.setEventId(faker.number().digits(10));
            List<AlarmsParams> list1 = new ArrayList<>();
            for (int j = 0; j < 2; j++) {
                AlarmsParams alarmsParams = new AlarmsParams();
                alarmsParams.setAlarmId(alarmId);
                alarmsParams.setParamKey(faker.number().numberBetween(1, 100));
                alarmsParams.setParamValue(faker.cat().name());
                list1.add(alarmsParams);
            }
            alarms.setAlarmsParams(list1);
            String s = JSONObject.toJSONString(alarms);
            String s1 = csvHandlerStr(s);
//            list.add(s1);
            appender.append(s1);

        }
        appender.flush();


//        writer.write(list);
//        writer.flush();
    }



    /**
     * 方法名称: csvHandlerStr</br>
     * 方法描述: 处理包含逗号，或者双引号的字段</br>
     * 方法参数: @param forecastName
     * 方法参数: @return  </br>
     * 返回类型: String</br>
     * 抛出异常:</br>
     */
    private static String csvHandlerStr(String str) {
        //csv格式如果有逗号，整体用双引号括起来；如果里面还有双引号就替换成两个双引号，这样导出来的格式就不会有问题了
        String tempDescription=str;
        //如果有逗号
        if(str.contains(",")){
            //如果还有双引号，先将双引号转义，避免两边加了双引号后转义错误
            if(str.contains("\"")){
                tempDescription=str.replace("\"", "\"\"");
            }
            //在将逗号转义
            tempDescription="\""+tempDescription+"\"";
        }
        return tempDescription;
    }
}